﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using Xeml.Manager;
using Xeml.Sampling.Contracts;
using Xeml.Document;
using Xeml.Sampling;

namespace CompilerControls
{
    public partial class DataProviderWizard : KryptonForm
    {
        private WizardState _state;

        private WizardState State
        {
            get { return _state; }
            set
            {
                _state = value;
                SetState();
            }
        }




        List<IDataProvider> _provider;
        public DataProviderWizard()
        {
            InitializeComponent();

            comboBoxProvider.SelectedValueChanged += new EventHandler(comboBoxProvider_SelectedValueChanged);
            int x = SpManager.Instance.ProviderComponents.Count;

            _provider = new List<IDataProvider>();
            foreach (IDataProvider prov in SpManager.Instance.ProviderComponents.Values)
            {
                IDataProvider provCopy = (IDataProvider)prov.Copy();
                _provider.Add(provCopy);
               // provCopy.AuthenticationRequest += new EventHandler<AuthenticationRequestEventArgs>(prov_AuthenticationRequest);
                
            }

            
            
            
            //comboBoxProvider.DisplayMember = "ComponentName";
            //comboBoxProvider.ValueMember = "Uri";
            comboBoxProvider.DataSource = _provider;// new BindingList<IDataProvider>(_provider);
            //comboBoxProvider.DisplayMember = "ComponentName";
            //comboBoxProvider.DataSource = _provider;// new BindingList<IDataProvider>(_provider);

            //var myobject = new Test();
            //comboBoxProvider.DataSource = new List<Test>(new Test[] { myobject });
            //comboBoxProvider.DisplayMember = "MyProperty";
            //comboBoxProvider.ValueMember = "MyProperty2";

            
        }

        //void prov_AuthenticationRequest(object sender, AuthenticationRequestEventArgs e)
        //{
        //    AuthenticationForm af = new AuthenticationForm();
        //    af.Setup(e);
        //    af.ShowDialog();
            
        //}

  
        void comboBoxProvider_SelectedValueChanged(object sender, EventArgs e)
        {
            IDataProvider prov = (IDataProvider)comboBoxProvider.SelectedValue;

            if (prov != null)
            {
                textBoxProviderName.Text = prov.ComponentName;
                kryptonLabelAuthor.Values.ExtraText = prov.Author;
                kryptonLabelDescription.Values.ExtraText = prov.Description;
                kryptonLabelUri.Values.ExtraText = prov.Uri;
                kryptonLabelPublisher.Values.ExtraText = prov.Publisher;
                kryptonLabelVersion.Values.ExtraText = prov.Version.ToString();
                kryptonLabelPopulating.Values.ExtraText = prov is IWriteableProvider ? "Yes" : "No";
               // kryptonLabelConnectionstring.Values.ExtraText = prov.SupportsConnectionStrBuild ? "Yes" : "No";

            }
        }


        private void ConfigureConnection()
        {
            IDataProvider prov = (IDataProvider)comboBoxProvider.SelectedValue;

            if (prov != null)
            {
              
               // prov.ConnectionStrProperties[0].
            }
        }

        private void Next()
        {
            switch (_state)
            {
                case WizardState.Welcome:
                    {
                        State = WizardState.ProviderSelect;
                        break;
                    }
                case WizardState.ProviderSelect:
                    {
                        State = WizardState.ConfigProvider;
                        break;
                    }
                case WizardState.ConfigProvider:
                    {
                        State = WizardState.Finish;
                        break;
                    }
                case WizardState.Finish:
                    break;
                default:
                    break;
            }
        }

        private void Back()
        {
            switch (_state)
            {
                case WizardState.Welcome:
                    {
                        State = WizardState.Welcome;

                        break;
                    }
                case WizardState.ProviderSelect:
                    {
                        State = WizardState.Welcome;
                        break;
                    }
                case WizardState.ConfigProvider:
                    {
                        State = WizardState.ProviderSelect;
                        break;
                    }
                case WizardState.Finish:
                    break;
                default:
                    break;
            }
        }

        private void SetState()
        {
            switch (_state)
            {
                case WizardState.Welcome:
                    {
                        kryptonNavigator.SelectedPage = kryptonPageWelcome;
                        kryptonButtonBack.Enabled = false;
                        break;
                    }
                case WizardState.ProviderSelect:
                    {
                        kryptonNavigator.SelectedPage = kryptonPageSelectProvider;
                        kryptonButtonBack.Enabled = true;
                        kryptonButtonNext.Text = "Next";
                        break;
                    }
                case WizardState.ConfigProvider:
                    {
                        //kryptonButton1.Visible = true;
                        kryptonNavigator.SelectedPage = kryptonPageConfigure;
                        ConfigureConnection();
                        kryptonButtonNext.Text = "Finish";
                        break;
                    }
                case WizardState.Finish:
                    {
                        this.DialogResult = DialogResult.OK;
                        BuildProviderResource();
                        Close();
                        break;
                    }
                default:
                    break;
            }
        }

        private void BuildProviderResource()
        {
             IDataProvider prov = (IDataProvider)comboBoxProvider.SelectedValue;
             SidMapping a = new SidMapping(1);
             a.ForeignKeyMap.Add("Experiment", "Adriano01");
             a.ForeignKeyMap.Add("Sample", "1");
            //prov.QueryData(new List<SidMapping>(new SidMapping[] {a}));

             if (prov != null)
             {
                 _providerRes = new DataProviderResource();
                 _providerRes.LoadComponent(prov.Uri);
                 //_providerRes.ConnectionString = prov.ConnectionString;
                 _providerRes.FriendlyName = textBoxProviderName.Text;
             }
             else
                 _providerRes = null;
        }

        private DataProviderResource _providerRes;
        public DataProviderResource ProviderResource
        {
            get { return _providerRes; }
        }

        private void kryptonButtonNext_Click(object sender, EventArgs e)
        {
            Next();
        }

        private void kryptonButtonBack_Click(object sender, EventArgs e)
        {
            Back();
        }

        private void kryptonButtonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void kryptonNavigator1_CloseAction(object sender, ComponentFactory.Krypton.Navigator.CloseActionEventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void kryptonNavigator1_NextAction(object sender, ComponentFactory.Krypton.Navigator.DirectionActionEventArgs e)
        {
            Next();
        }

        private void kryptonPageConfigure_Click(object sender, EventArgs e)
        {

        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            IDataProvider prov = (IDataProvider)comboBoxProvider.SelectedValue;
            if (prov != null)
            {
               //ConnectResult res = prov.TestConnection();
               //switch (res)
               //{
                 
               //    case ConnectResult.Cancelled:
               //        {
               //            MessageBox.Show("Test aborted", "Test abored", MessageBoxButtons.OK, MessageBoxIcon.Error);
               //            break;
               //        }
               //    case ConnectResult.Success:
               //        {
               //            MessageBox.Show("Test successful", "Test OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
               //            break;
               //        }
               //    case ConnectResult.Failed:
               //        {
               //            MessageBox.Show("Test failed", "Test failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
               //            break;
               //        }
               //    default:
               //        break;
              // }
            }
        }
    }

    internal enum WizardState
    {
        Welcome,
        ProviderSelect,
        ConfigProvider,
        Finish
    }
}
